home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Appearance SDK 1.0.4 / Appearance Sample Code / Source / UDialogUtils.cp < prev    next >
Encoding:
Text File  |  1999-07-16  |  6.6 KB  |  239 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        UDialogUtils.cp
  3.  
  4.     Contains:    Dialog item utilities
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (edv)    Ed Voas
  21.  
  22.     Change History (most recent first):
  23.  
  24.          <1>     9/11/97    edv        First checked in.
  25. */
  26.  
  27.  
  28. #include "UDialogUtils.h"
  29.  
  30. //———————————————————————————————————————————————————————————————————————————
  31. //    • GetDialogItemHandle
  32. //———————————————————————————————————————————————————————————————————————————
  33. //    Returns the handle to the specified dialog item.
  34. //
  35. Handle
  36. UDialogUtils::GetDialogItemHandle( DialogPtr theDialog, SInt16 item )
  37. {
  38.     SInt16        itemType;
  39.     Handle        itemHand;
  40.     Rect        itemRect;
  41.     
  42.     ::GetDialogItem( theDialog, item, &itemType, &itemHand, &itemRect );
  43.     return itemHand;
  44. }
  45.  
  46. //———————————————————————————————————————————————————————————————————————————
  47. //    • SetDialogItemHandle
  48. //———————————————————————————————————————————————————————————————————————————
  49. //    Sets the handle of the specified dialog item to the handle given.
  50. //
  51. void
  52. UDialogUtils::SetDialogItemHandle( DialogPtr theDialog, SInt16 item, Handle handle )
  53. {
  54.     SInt16        itemType;
  55.     Handle        itemHand;
  56.     Rect        itemRect;
  57.     
  58.     ::GetDialogItem( theDialog, item, &itemType, &itemHand, &itemRect );
  59.     ::SetDialogItem( theDialog, item, itemType, handle, &itemRect );
  60. }
  61.  
  62. //———————————————————————————————————————————————————————————————————————————
  63. //    • GetDialogItemRect
  64. //———————————————————————————————————————————————————————————————————————————
  65. //    Gets the bounding rectangle of the specified dialog item.
  66. //
  67. void
  68. UDialogUtils::GetDialogItemRect( DialogPtr theDialog, SInt16 item, Rect& rect )
  69. {
  70.     SInt16        itemType;
  71.     Handle        itemHand;
  72.     
  73.     ::GetDialogItem( theDialog, item, &itemType, &itemHand, &rect );
  74. }
  75.  
  76. //———————————————————————————————————————————————————————————————————————————
  77. //    • SetDialogItemRect
  78. //———————————————————————————————————————————————————————————————————————————
  79. //    Sets the bounding rectangle of the specified dialog item to the given rectangle.
  80. //
  81. void
  82. UDialogUtils::SetDialogItemRect( DialogPtr theDialog, SInt16 item, const Rect& rect )
  83. {
  84.     SInt16        itemType;
  85.     Handle        itemHand;
  86.     Rect        itemRect;
  87.     
  88.     ::GetDialogItem( theDialog, item, &itemType, &itemHand, &itemRect );
  89.     ::SetDialogItem( theDialog, item, itemType, itemHand, &rect );
  90. }
  91.  
  92. //———————————————————————————————————————————————————————————————————————————
  93. //    • FlashDialogItem
  94. //———————————————————————————————————————————————————————————————————————————
  95. //    This routine is used to flash a button for the cases when the user presses a key that
  96. //    'clicks' a dialog button. If the item specified is not a button, the routine does nothing.
  97. //
  98. void
  99. UDialogUtils::FlashDialogItem( DialogPtr theDialog, SInt16 item )
  100. {
  101.     ControlHandle    control;
  102.     DELAY_LONG        ticks;
  103.     OSErr            err;
  104.     
  105.     err = ::GetDialogItemAsControl( theDialog, item, &control );
  106.     if ( err == noErr )
  107.     {
  108.         HiliteControl( control, 1 );
  109.         Delay( 8, &ticks );
  110.         HiliteControl( control, 0 );
  111.     }
  112. }
  113.  
  114. //———————————————————————————————————————————————————————————————————————————
  115. //    • ToggleCheckBox
  116. //———————————————————————————————————————————————————————————————————————————
  117. //    This routine simply toggles a check box.
  118. //
  119. void
  120. UDialogUtils::ToggleCheckBox( DialogPtr dialog, SInt16 item )
  121. {
  122.     SInt16    newState = 0;
  123.     
  124.     if ( GetDialogItemValue( dialog, item ) == 0 )
  125.         newState = 1;
  126.     SetDialogItemValue( dialog, item, newState );
  127. }
  128.  
  129. //———————————————————————————————————————————————————————————————————————————
  130. //    • SetDialogItemValue
  131. //———————————————————————————————————————————————————————————————————————————
  132. //    This routine sets the value of the given item to the value passed in.
  133. //
  134. void
  135. UDialogUtils::SetDialogItemValue( DialogPtr dialog, SInt16 item, SInt16 value )
  136. {
  137.     ControlHandle    control;
  138.     OSErr            err;
  139.         
  140.     err = ::GetDialogItemAsControl( dialog, item, &control );
  141.     if ( err ) return;
  142.     
  143.     ::SetControlValue( control, value );
  144. }
  145.  
  146. //———————————————————————————————————————————————————————————————————————————
  147. //    • GetDialogItemValue
  148. //———————————————————————————————————————————————————————————————————————————
  149. //    This routine gets the value of the given item and returns it.
  150. //
  151. SInt16
  152. UDialogUtils::GetDialogItemValue( DialogPtr dialog, SInt16 item )
  153. {
  154.     ControlHandle    control;
  155.     OSErr            err;
  156.     
  157.     err = ::GetDialogItemAsControl( dialog, item, &control );
  158.     if ( err ) return 0;
  159.     
  160.     return ::GetControlValue( control );
  161. }
  162.  
  163. //———————————————————————————————————————————————————————————————————————————
  164. //    • SetDialogItemText
  165. //———————————————————————————————————————————————————————————————————————————
  166. //    This routine sets the text of the given item to the value passed in.
  167. //
  168. void
  169. UDialogUtils::SetDialogItemText( DialogPtr dialog, SInt16 item, StringPtr text )
  170. {
  171.     SInt16        itemType;
  172.     Handle        itemHand;
  173.     Rect        itemRect;
  174.     ControlHandle    root;
  175.     
  176.     if ( GetRootControl( dialog, &root ) == noErr )
  177.         ::GetDialogItemAsControl( dialog, item, (ControlHandle*)&itemHand );
  178.     else
  179.         ::GetDialogItem( dialog, item, &itemType, &itemHand, &itemRect );
  180.     ::SetDialogItemText( itemHand, text );
  181. }
  182.  
  183. //———————————————————————————————————————————————————————————————————————————
  184. //    • GetDialogItemText
  185. //———————————————————————————————————————————————————————————————————————————
  186. //    This routine gets the text of the given item and returns it.
  187. //
  188. void
  189. UDialogUtils::GetDialogItemText( DialogPtr dialog, SInt16 item, StringPtr text )
  190. {
  191.     SInt16        itemType;
  192.     Handle        itemHand;
  193.     Rect        itemRect;
  194.     
  195.     ::GetDialogItem( dialog, item, &itemType, &itemHand, &itemRect );
  196.     ::GetDialogItemText( itemHand, text );
  197. }
  198.  
  199.  
  200. //———————————————————————————————————————————————————————————————————————————
  201. //    • EnableDialogItem
  202. //———————————————————————————————————————————————————————————————————————————
  203. //    This routine enables or disables a specified item. enableIt is true for enabling, false
  204. //    to disable.
  205. //
  206. void
  207. UDialogUtils::EnableDialogItem( DialogPtr dialog, SInt16 item, Boolean enableIt )
  208. {
  209.     ControlHandle        control;
  210.     OSErr                err;
  211.     
  212.     err = ::GetDialogItemAsControl( dialog, item, &control );
  213.     if ( err ) return;
  214.     
  215.     if ( enableIt )
  216.         ::ActivateControl( control );
  217.     else
  218.     {
  219.         ::DeactivateControl( control );
  220.     }
  221. }
  222.  
  223. //———————————————————————————————————————————————————————————————————————————
  224. //    • SetFontStyle
  225. //———————————————————————————————————————————————————————————————————————————
  226. //    This routine sets the font style of a dialog item.
  227. //
  228. void
  229. UDialogUtils::SetFontStyle( DialogPtr dialog, SInt16 item, ControlFontStyleRec& style )
  230. {
  231.     ControlHandle     control;
  232.     OSErr            err;
  233.     
  234.     err = ::GetDialogItemAsControl( dialog, item, &control );
  235.     
  236.     if ( err == noErr )
  237.         ::SetControlFontStyle( control, &style );
  238. }
  239.